import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/home',
    name: 'home',
    redirect:'/home/backstage',
    meta:{
      needlogin:true
    },
    component: () => import( /* webpackChunkName: "home" */ '../views/home/Home.vue'),
    children: [{
        path: 'backstage',
        name: 'backstage',
        meta:{
          needlogin:true
        },
        component: () => import('../components/home/backstage.vue')
      },
      {
        path: 'goodslist',
        name: 'goodslist',
        meta:{
          needlogin:true
        },
        component: () => import('../components/home/goodslist.vue')
      },
      {
        path: 'album',
        name: 'album',
        meta:{
          needlogin:true
        },
        component: () => import('../components/home/album.vue')
      },
    ]
  },
  {
    path: '/goods',
    name: 'goods',
    redirect:'/goods/goodsdata',
    meta:{
      needlogin:true
    },
    component: () => import('../views/goods/goods.vue'),
    children: [{
        path: 'goodsdata',
        name: 'goodsdata',
        meta:{
          needlogin:true
        },
        component: () => import('../components/goods/goodsdata.vue')
      },
      {
        path: 'classify',
        name: 'classify',
        meta:{
          needlogin:true
        },
        component: () => import('../components/goods/classify.vue')
      },
      {
        path: 'goodsspecs',
        name: 'goodsspecs',
        meta:{
          needlogin:true
        },
        component: () => import('../components/goods/goodsspecs.vue')
      },
      {
        path: 'goodstype',
        name: 'goodstype',
        meta:{
          needlogin:true
        },
        component: () => import('../components/goods/goodstype.vue')
      },
      {
        path: 'goodscomment',
        name: 'goodscomment',
        meta:{
          needlogin:true
        },
        component: () => import('../components/goods/goodscomment.vue')
      }

    ]
  },
  {
    path: '/oder',
    name: 'oder',
    redirect:'/oder/odermg',
    meta:{
      needlogin:true
    },
    component: () => import('../views/oder/oder.vue'),
    children:[
      {
        path: 'odermg',
        name: 'odermg',
        meta:{
          needlogin:true
        },
        component: () => import('../components/oder/odermg.vue')
      },
      {
        path: 'datamg',
        name: 'datamg',
        meta:{
          needlogin:true
        },
        component: () => import('../components/oder/datamg.vue')
      },
      {
        path: 'service',
        name: 'service',
        meta:{
          needlogin:true
        },
        component: () => import('../components/oder/service.vue')
      },
    ]
  },
  {
    path: '/member',
    name: 'member',
    redirect:'/member/membermg',
    meta:{
      needlogin:true
    },
    component: () => import('../views/member/member.vue'),
    children:[
      {
        path: 'memberdj',
        name: 'memberdj',
        meta:{
          needlogin:true
        },
        component: () => import('../components/member/memberdj.vue')
      },
      {
        path: 'membermg',
        name: 'membermg',
        meta:{
          needlogin:true
        },
        component: () => import('../components/member/membermg.vue')
      },
    ]
  },
  {
    path: '/myset',
    name: 'myset',
    redirect:'/myset/mysetjc',
    meta:{
      needlogin:true
    },
    component: () => import('../views/myset/myset.vue'),
    children:[
      {
        path: 'mysetjc',
        name: 'mysetjc',
        meta:{
          needlogin:true
        },
        component: () => import('../components/myset/mysetjc.vue')
      },
      {
        path: 'mysetjy',
        name: 'mysetjy',
        meta:{
          needlogin:true
        },
        component: () => import('../components/myset/mysetjy.vue')
      },
      {
        path: 'mysetgl',
        name: 'mysetgl',
        meta:{
          needlogin:true
        },
        component: () => import('../components/myset/mysetgl.vue')
      },
      {
        path: 'mysetwl',
        name: 'mysetwl',
        meta:{
          needlogin:true
        },
        component: () => import('../components/myset/mysetwl.vue')
      },
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
import $store from '../store'
router.beforeEach((to,from,next)=>{
  if(to.meta.needlogin){
    if($store.state.user.username!=''){
      next()
    }else{
      next({
        path:'/login'
      })
    }
  }else{
    next()
  }
})

export default router